<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
	include('../../includes/classes/core.php');
	$action = $_REQUEST['act'];
	$start		=	$_REQUEST['start'];
    $end		=	$_REQUEST['end'];
	$error = '';
	$data = '';
	
	switch ($action) {
		case 'get_edit_page':
			$transfer   		= $_REQUEST['id'];

			if($transfer[0] == 'p'){
				$page				= GetPosPage(substr($transfer,1));
				$data				= array('page'	=> $page);
			}else{
				$page				= GetPage($transfer);
				$data				= array('page'	=> $page);
			}
		
			break;
			
	    case 'get_list':
    	    $count = $_REQUEST['count'];
    	    
    	    $rResult = mysql_query("SELECT         	report.id,
													DATE(report.date) AS `date`,
													TIME(report.date) AS `time`,
													report.`in`,
													report.`out`,
													report.car,
													report.payed_money,
													report.coin,
    	    										report.pay_type,
													report.person,
													report.object,
													report.parent,
													report.`comment`
									FROM			(SELECT 	sale_transaction.id AS `id`,
																sale_transaction.end_date AS `date`,
																ROUND(sale_transaction.sum_cost, 2) AS `in`,
																'' AS `out`,
																sale_transaction.car_number AS `car`,
																ROUND(sale_transaction.payed_money, 2) AS `payed_money`,
																ROUND(sale_transaction.payed_money - sale_transaction.sum_cost, 2) AS `coin`,
																IF(sale_transaction.pay_type = 1, 'ნაღდი', 'უნაღდო') AS `pay_type`,
																persons.`name` AS `person`,
																obj1.`name` AS `object`,
																obj2.`name` AS `parent`,
																'' AS `comment`
													FROM 		`sale_transaction`
													LEFT JOIN 	users ON sale_transaction.user_id = users.id
													LEFT JOIN 	persons ON users.person_id = persons.id
													LEFT JOIN 	object  AS `obj1` ON sale_transaction.object_id = obj1.id
													LEFT JOIN 	object  AS `obj2` ON obj1.parent = obj2.id
													WHERE     	(sale_transaction.end_date  BETWEEN  '$start'  AND '$end') AND (sale_transaction.`status` = 0)
													UNION ALL
													SELECT 		CONCAT('p',pos_money_action.id) AS `id`,
																pos_money_action.date AS `date`,
																IF(pos_money_action.action = 1, pos_money_action.money, '') AS `in`,
																IF(pos_money_action.action = 2, pos_money_action.money, '') AS `out`,
																'' AS `car`,
																'' AS `payed_money`,
																'' AS `coin`,
    	    													'ნაღდი' AS `pay_type`,
																persons.`name` AS `person`,
																obj1.`name` AS `object`,
																obj2.`name` AS `parent`,
																pos_money_action.`comment` AS `comment`
													FROM 		`pos_money_action`
													LEFT JOIN 	users ON pos_money_action.user_id = users.id
													LEFT JOIN 	persons ON users.person_id = persons.id
													LEFT JOIN 	object  AS `obj1` ON pos_money_action.pos_id = obj1.id
													LEFT JOIN 	object  AS `obj2` ON obj1.parent = obj2.id
													WHERE     	pos_money_action.date  BETWEEN  '$start'  AND '$end') AS report
									ORDER BY 		report.date DESC
									");
			
			$data = array(
				"aaData"	=> array()
			);
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$data['aaData'][] = $row;
			}
			
			

	        break;	

	 case 'get-info' :
	 	
	 	$start_date     = $_REQUEST['start_date'];
	 	$end_date   	= $_REQUEST['end_date'];


 	 	$page				= GetInfoPage($start_date, $end_date);
  	 	$data				= array('page'	=> $page);	 	
	 	
	 	break;
	 	
	    default:
	       	echo "null";
	}
	
	$data['error'] = $error;
	
	echo json_encode($data);
	
	function GetInfoPage($start_date, $end_date){

		$res = mysql_query("SELECT	 ROUND(SUM(sale_transaction.sum_cost), 2) AS `money`
							FROM    sale_transaction
							WHERE sale_transaction.end_date >= '$start_date' AND sale_transaction.end_date <= '$end_date' AND  sale_transaction.pay_type = 1 AND sale_transaction.`status` = 0
							
							UNION ALL
							
							SELECT ROUND(SUM(sale_transaction.sum_cost), 2) AS `money`
							FROM    sale_transaction
							WHERE sale_transaction.end_date >= '$start_date' AND sale_transaction.end_date <= '$end_date' AND  sale_transaction.pay_type = 2 AND sale_transaction.`status` = 0
							
							UNION ALL
							
							SELECT ROUND(SUM(sale_transaction.sum_cost), 2) AS `money`
							FROM    sale_transaction
							WHERE sale_transaction.end_date >= '$start_date' AND sale_transaction.end_date <= '$end_date' AND sale_transaction.`status` = 0");
		$sales = array();
		
		while($row = mysql_fetch_assoc($res)){
			$sales[] = $row['money']; 
		}
		
		
		$res2 = mysql_query("SELECT SUM(pos_money_action.money) AS `custom_money`
							FROM    pos_money_action
							WHERE	pos_money_action.action = 1 AND pos_money_action.date >= '$start_date' AND pos_money_action.date <= '$end_date' 
							
							UNION ALL
							
							SELECT  SUM(pos_money_action.money) AS `custom_money`
							FROM    pos_money_action
							WHERE   pos_money_action.action = 2 AND pos_money_action.date >= '$start_date' AND pos_money_action.date <= '$end_date'");
		
		$custom_money = array();
		
		while($row2 = mysql_fetch_assoc($res2)){
			$custom_money[] = $row2['custom_money']; 
		}
		
		
		$res3 = mysql_query("SELECT	ROUND(SUM(sale_transaction.sum_cost), 2) AS `money`
							FROM    sale_transaction
							WHERE 	sale_transaction.end_date >= '$start_date' AND sale_transaction.end_date <= '$end_date' AND sale_transaction.object_id = 136 AND sale_transaction.pay_type = 1
								
							UNION ALL
								
							SELECT ROUND(SUM(sale_transaction.sum_cost), 2) AS `money`
							FROM   sale_transaction
							WHERE  sale_transaction.end_date >= '$start_date' AND sale_transaction.end_date <= '$end_date' AND sale_transaction.object_id = 136 AND  sale_transaction.pay_type = 2
								
							UNION ALL
								
							SELECT ROUND(SUM(sale_transaction.sum_cost), 2) AS `money`
							FROM   sale_transaction
							WHERE  sale_transaction.end_date >= '$start_date' AND sale_transaction.end_date <= '$end_date' AND sale_transaction.object_id = 136");
		
		$drive_sales = array();
		
		while($row3 = mysql_fetch_assoc($res3)){
			$drive_sales[] = $row3['money'];
		}
		
		
		$res4 = mysql_query("SELECT	ROUND(SUM(sale_transaction.sum_cost), 2) AS `w_money`
							FROM    sale_transaction
							WHERE 	sale_transaction.end_date >= '$start_date' AND sale_transaction.end_date <= '$end_date' AND sale_transaction.object_id = 138 AND sale_transaction.pay_type = 1
					
							UNION ALL
					
							SELECT ROUND(SUM(sale_transaction.sum_cost), 2) AS `w_money`
							FROM   sale_transaction
							WHERE  sale_transaction.end_date >= '$start_date' AND sale_transaction.end_date <= '$end_date' AND sale_transaction.object_id = 138 AND  sale_transaction.pay_type = 2
					
							UNION ALL
					
							SELECT ROUND(SUM(sale_transaction.sum_cost), 2) AS `w_money`
							FROM   sale_transaction
							WHERE  sale_transaction.end_date >= '$start_date' AND sale_transaction.end_date <= '$end_date' AND sale_transaction.object_id = 138");
		
		$warehouse_sales = array();
		
		while($row4 = mysql_fetch_assoc($res4)){
			$warehouse_sales[] = $row4['w_money'];
		}
		
		
		$rotation = $sales[2] + $custom_money[0] - $custom_money[1];
		$profit   = $sales[2] + $custom_money[0];
		$expense  = $custom_money[1];
		
														
		$data = '';
		
		$data .= '    <table style="border-right:1px solid gray; padding-left:7px; float:left; display:inline;">
            				<tr>
            					<td style="text-align:right;">გაყიდვები: </td>
            					<td><pre style="font-weight:bold;" class="sales" title="თანხა გვიჩვენებს ობიექტის ჯამურ გაყიდვებს გაფილტრული პერიოდის მიხედვით"> '.$sales[2].' ლ</pre></td>
            				</tr>
            				<tr>
            					<td  style="padding-top:10px; padding-left:10px; text-align:right;">ნაღდი: </td>
            					<td  style="padding-top:10px;"><pre style="font-weight:bold;" class="sales" title="თანხა გვიჩვენებს ობიექტის ნაღდი ანგარიშწორებით  გაყიდვებს გაფილტრული პერიოდის მიხედვით"> '.$sales[0].' ლ</pre></td>
            				</tr>
            				<tr>
            					<td  style="padding-top:10px; text-align:right;">უნაღდო: </td>
            					<td  style="padding-top:10px; padding-right:10px;"><pre style="font-weight:bold;" class="sales" title="თანხა გვიჩვენებს ობიექტის უნაღდი ანგარიშწორებით  გაყიდვებს გაფილტრული პერიოდის მიხედვით"> '.$sales[1].' ლ</pre></td>
            				</tr>
            			</table>
            			
            	       <table style="border-right:1px solid gray; padding-left:7px; float:left; display:inline;">
            				<tr>
            					<td style="text-align:right;">დრაივის გაყიდვები: </td>
            					<td><pre style="font-weight:bold;"> '.$drive_sales[2].' ლ</pre></td>
            				</tr>
            				<tr>
            					<td  style="padding-top:10px; padding-left:10px; text-align:right;">ნაღდი: </td>
            					<td  style="padding-top:10px;"><pre style="font-weight:bold;"> '.$drive_sales[0].' ლ</pre></td>
            				</tr>
            				<tr>
            					<td  style="padding-top:10px; text-align:right;">უნაღდო: </td>
            					<td  style="padding-top:10px; padding-right:10px;"><pre style="font-weight:bold;"> '.$drive_sales[1].' ლ</pre></td>
            				</tr>
            			</table>
            			
            			<table style="border-right:1px solid gray; padding-left:7px; float:left; display:inline;">
            				<tr>
            					<td style="text-align:right;">დარბაზის გაყიდვები: </td>
            					<td><pre style="font-weight:bold;"> '.$warehouse_sales[2].' ლ</pre></td>
            				</tr>
            				<tr>
            					<td  style="padding-top:10px; padding-left:10px; text-align:right;">ნაღდი: </td>
            					<td  style="padding-top:10px;"><pre style="font-weight:bold;"> '.$warehouse_sales[0].' ლ</pre></td>
            				</tr>
            				<tr>
            					<td  style="padding-top:10px; text-align:right;">უნაღდი: </td>
            					<td  style="padding-top:10px; padding-right:10px;"><pre style="font-weight:bold;"> '.$warehouse_sales[1].' ლ</pre></td>
            				</tr>
            			</table>

            			<table style="border-right:1px solid gray; padding-left:7px; float:left; display:inline;">
            				<tr>
            					<td  style="text-align:right;" >სულ შემოსავალი: </td>
            					<td><pre style="font-weight:bold;" class="sales" title="თანხა გვიჩვენებს ნავაჭრის(როგორც ნაღდი, ასევე უნაღდო) და სხვა ნებისმიერი შემოსავლის ჯამს"> '.$profit.' ლ</pre></td>
            				</tr>
            				<tr>
            					<td  style="padding-top:10px; text-align:right;">სულ გასავალი: </td>
            					<td  style="padding-top:10px; padding-right:10px;"><pre style="font-weight:bold;" class="sales" title="თანხა გვიჩვენებს სალაროდან გასული თანხის ჯამს"> '.$expense.' ლ</pre></td>
            				</tr>
             				<tr>
            					<td style="text-align:right; padding-top:10px; ">ბალანსი: </td>
            					<td><pre style="font-weight:bold; padding-top:10px;" > '.$rotation.' ლ</pre></td>
            				</tr>           							
            			</table>
            			
            			
            			
            			 <table style="border-right:1px solid gray; padding-left:7px; float:left; display:inline;">
            				<tr>
            					<td  style="padding-left:10px; text-align:right;" >სხვა შემოსავალი: </td>
            					<td><pre style="font-weight:bold;" class="sales" title="თანხა გვიჩვენებს სალაროში შემოტანილი თანხის ჯამს(არა გაყიდვებიდან)"> '.$custom_money[0].' ლ</pre></td>
            				</tr>
            				<tr>
            					<td  style="padding-top:10px; text-align:right;">გასავალი: </td>
            					<td  style="padding-top:10px; padding-right:10px;"><pre style="font-weight:bold;" class="sales" title="თანხა გვიჩვენებს სალაროდან გატანილი თანხის ჯამს"> '.$custom_money[1].' ლ</pre></td>
            				</tr>
            			</table>';
		
		return $data;
		
	}
	
	function GetPage($transfer)
	{
		$data = '
		<div id="dialog-form">
			<fieldset>
				<legend>ინფორმაცია</legend>
	
		    	<table class="dialog-form-table">
					<tr>
						<td style="width: 170px;"><label for="op_date">ოპერაციის თარიღი</label></td>
						<td>
							<span id="op_date" style="font-weight:bold; font-size:15px;">'.GetTransferDate($transfer).'</span>
						</td>
				 	</tr>
				 	
						' .GetInfo($transfer) .'
				
				</table>
				<!-- ID -->
				<input type="hidden" id="transfer_id" value="'.$transfer.'" />
	        </fieldset>
    	</div>
    ';
		return $data;
	}
	
	function GetPosPage($transfer)
	{
		$data = '
		<div id="dialog-form">
			<fieldset>
				<legend>ინფორმაცია</legend>
	
		    	<table class="dialog-form-table">
					<tr>
						<td style="width: 170px;"><label for="op_date">ოპერაციის თარიღი</label></td>
						<td>
							<span id="op_date" style="font-weight:bold; font-size:15px;">'.GetPosDate($transfer).'</span>
						</td>
				 	</tr>			
				    <tr>
						' .GetPosInfo($transfer) .'
					</tr>
				</table>
				<!-- ID -->
				<input type="hidden" id="transfer_id" value="p'.$transfer.'" />
	        </fieldset>
    	</div>
    ';
		return $data;
	}

	
	function GetInfo($transfer){
	    $data = '';
	    
	    $res1 = mysql_fetch_assoc(mysql_query("SELECT  `sale_transaction`.`sum_cost` AS `sum_cost`,
												       `sale_transaction`.`car_number` AS `car_number`,
												       `sale_transaction`.`payed_money` AS `payed_money`,
													   `persons`.`name` AS `person`,
													   ROUND(`sale_transaction`.`payed_money`-`sale_transaction`.`sum_cost`,2) AS `xurda`
												FROM `sale_transaction`
												LEFT JOIN 	`users` ON `sale_transaction`.`user_id` = `users`.`id`
												LEFT JOIN 	`persons` ON `users`.`person_id` = `persons`.`id`
												WHERE `sale_transaction`.`id` = $transfer"));
	    
	    $data .= '
	    			 <tr>
						<td style="width: 170px;"><label for="cost">შემოსავალი:</label></td>
						<td>
							<span id="cost" style="font-weight:bold; ">'.$res1['sum_cost'].' ლ</span>
						</td>
				 	 </tr>
					<tr>
						<td style="width: 170px;"><label for="cost">გადახდილი თანხა:</label></td>
						<td>
							<span id="cost" style="font-weight:bold; ">'.$res1['payed_money'].' ლ</span>
						</td>
				 	 </tr>	
					 <tr>
						<td style="width: 170px;"><label for="xurda">ხურდა:</label></td>
						<td>
							<span id="xurda" style="font-weight:bold; ">'.$res1['xurda'].' ლ</span>
						</td>
				 	 </tr>		
					 <tr>
					<td style="width: 170px;"><label for="person">მომხმარებელი:</label></td>
						<td>
							<span id="person" style="font-weight:bold; ">'.$res1['person'].'</span>
						</td>
				 	 </tr>										
					<td style="width: 170px;"><label for="car_numb">ავტ.ნომერი:</label></td>
						<td>
							<span id="car_numb" style="font-weight:bold; ">'.$res1['car_number'].'</span>
						</td>
				 	 </tr>															
	    		';
		
		$res = mysql_query("SELECT 		`sale_transaction_detail`.`id` AS `id`,
												`production`.`name` AS `name`,
												`sale_transaction_detail`.`cost` AS `cost`,
												`sale_transaction_detail`.`product_quantity` AS `quantity`,
												 ROUND(`sale_transaction_detail`.`cost` * `sale_transaction_detail`.`product_quantity`, 2 ) AS `sumCost`
									FROM 		`sale_transaction` 
									LEFT JOIN 	`sale_transaction_detail`
									ON 			`sale_transaction`.`id` = `sale_transaction_detail`.`transaction_id`
									LEFT JOIN 	`production`
									ON 			`sale_transaction_detail`.`production_id` = `production`.`id`
									WHERE   	`sale_transaction`.`id` = $transfer");
		
		$sum = mysql_fetch_assoc(mysql_query("SELECT  `sale_transaction`.`sum_cost` AS `sumCost`
												FROM  `sale_transaction`
												WHERE `sale_transaction`.`id` = $transfer"));
		
		$data .='<table style="margin-bottom:20px; width:450px;">';
		
		$data .= '<tr class="restore_list_header">
					  <td style="display:none;">&nbsp;</td>
					  <td class="restore_list_header">სახელი</td>
					  <td class="restore_list_header">რაოდ.</td>
			          <td class="restore_list_header">ერთ.ფასი</td>
					  <td class="restore_list_header">ჯ.ფასი</td>
				  </tr>';
		

		while($row = mysql_fetch_assoc($res)){
			
			$data .= '<tr>
					  <td style="display:none;">'.$row['id'].'</td>
					  <td class="restore_list"><div class="o_name">'.$row['name'].'</div></td>  
					  <td class="restore_list">'. $row['quantity'] .'</td>
					  <td class="restore_list">'.$row['cost'].'</td>
					  <td class="restore_list">'.$row['sumCost'].'</td>				  		
				  </tr>';
		}
		
		$data .= '<tr>
				<td  colspan="3">&nbsp;</td>
				<td  style="border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; text-align:center; !important; font-size:15px; background-color:tomato; padding-top:10px; padding-bottom:10px;">სულ:&nbsp;<span id="sum-price2" style="font-size:15px; font-weight:bold;">'.$sum['sumCost'] .'</span></td>
				</tr>';
		
		$data .= '</table>';
		
		return $data;
	}
	
	function GetPosInfo($transfer){
		$data ='<table style="margin-bottom:20px; width:450px;">';
		
		$res = mysql_fetch_assoc(mysql_query("	SELECT 		CONCAT('p',pos_money_action.id) AS `id`,
																pos_money_action.date AS `date`,
																IF(pos_money_action.action = 1, 'შეტანა', 'გატანა') AS `act`,
																pos_money_action.money AS `money`,
    	    													'ნაღდი' AS `pay_type`,
																persons.`name` AS `person`,
																obj1.`name` AS `object`,
																obj2.`name` AS `parent`,
																pos_money_action.`comment` AS `comment`
													FROM 		`pos_money_action`
													LEFT JOIN 	users ON pos_money_action.user_id = users.id
													LEFT JOIN 	persons ON users.person_id = persons.id
													LEFT JOIN 	object  AS `obj1` ON pos_money_action.pos_id = obj1.id
													LEFT JOIN 	object  AS `obj2` ON obj1.parent = obj2.id
												WHERE `pos_money_action`.`id` = $transfer"));
	
		$data .= '<tr class="restore_list_header" style="background:none !important;">  
					  <td class="restore_list_header" style="background:none !important; color:gray;">ოპერაცია</td>	  
					  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['act'].'</td>
				  </tr>	
				  <tr class="restore_list_header" style="background:none !important;">  
					  <td class="restore_list_header" style="background:none !important; color:gray;">რაოდენობა</td>	  
					  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['money'].' ლ</td>
				  </tr>						  		
				  <tr class="restore_list_header" style="background:none !important;">  
					  <td class="restore_list_header" style="background:none !important; color:gray;">მომხმარებელი</td>	  
					  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['person'].'</td>
				  </tr>	
				  <tr class="restore_list_header" style="background:none !important;">  
					  <td class="restore_list_header" style="background:none !important; color:gray;">ობიექტი</td>	  
					  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['object'].'</td>
				  </tr>						  		
				  <tr class="restore_list_header" style="background:none !important;">  
					  <td class="restore_list_header" style="background:none !important; color:gray;">ტიპი</td>	  
					  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['pay_type'].'</td>
				  </tr>
				  <tr>
					  <td colspan="2" style="text-align:center; padding:15px 0 5px 0;">
					  		კომენტარი
					  </td>		 
				  </tr> 						  			
				  <tr>
					  <td colspan="2">
					  		<textarea rows="4" cols="61" style="resize: none;" disabled>'.$res['comment'].'</textarea>
					  </td>		 
				  </tr> 				
				';
	
		$data .= '</table>';
	
		return $data;
	}
	

	function GetTransferDate($transfer){
		$res = mysql_fetch_assoc(mysql_query("SELECT  `sale_transaction`.`end_date` AS `end_date`
												FROM  `sale_transaction`
												WHERE `sale_transaction`.`id` = $transfer"));
		
		return $res['end_date'];
	
	}
	
	function GetPosDate($transfer){
		$res = mysql_fetch_assoc(mysql_query("SELECT   `pos_money_action`.`date` AS `date`
												FROM   `pos_money_action`
												WHERE  `pos_money_action`.`id` = $transfer"));
	
		return $res['date'];
	
	}

?>